<template>
  <div>
    <el-card style="margin-top:20px;">
      <h3>品类管理--添加品类</h3>
      <el-form ref="form" label-width="80px">
        <el-form-item label="所属分类">
          <el-select v-model="selectId" placeholder="请选择一级品类">
            <el-option
              v-for="item in selectList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品类名称">
          <el-input v-model="categoryName"></el-input>
        </el-form-item>
      </el-form>
      <el-button @click="addCategory()">提交</el-button>
    </el-card>
  </div>
</template>

<script>
import { productType } from "../util/http";
import { categoryAdd } from "../util/http";
export default {
  // 组件名称
  name: "demo", // 组件参数 接收来自父组件的数据
  props: [],
  components: {},
  data() {
    return {
      selectList: [],
      selectId: "",
      categoryName: ""
    };
  },
  computed: {},
  watch: {},
  methods: {
    async getType() {
      const { data: res } = await productType(
        `/api/manage/category/get_category.do?categoryId=0`
      );
      if (res.status !== 0) return this.$message.error("获取数据失败");
      this.$message.success("获取数据成功");
      this.selectList = res.data;
    },
    async addCategory() {
      const {data:res} = await categoryAdd(`/api/manage/category/add_category.do?parentId=${this.selectId}&categoryName=${this.categoryName}`)
      if (res.status !== 0) return this.$message.error("添加品类失败");
      this.$message.success("添加品类成功");
      this.$router.push('/charts')
    }
  },
  beforeCreate() {},
  created() {},
  beforeMount() {},
  mounted() {
    this.getType();
  }
};
</script> 
<style scoped>
</style>
